﻿var trunPageObj;//翻页组件对像
var fromEleIds = "#branchNoIn,#exchangeTypeIn,#stockAccountIn,#stockCodeIn,#fundAccountIn,#taCodeIn,#dzAmountIn,#hsAmountIn,#diffTypeIn,#noteIn";
var isModify;//是否修改
$(initPage);
function initPage() {
	//初始化翻页组件
    trunPageObj =  $(".pageNext").turnPage(12, exportList);
	
	$("#query").click(function() {
		$("#data_list").empty();
		trunPageObj.gotoPage(1);//搜索第一页
		$("#btnCancle").click();
	});
	trunPageObj.gotoPage(1);//搜索第一页
	
	//新增
	$("#btnAdd").click(function() {
		$("#btnAdd,#btnModify,#btnDelete").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled", false);
		$(fromEleIds).attr("disabled", false);
		
		isModify = false;
		clearValues();
	});
	
	//取消
	$("#btnCancle").click(function() {
		$("#btnSave,#btnModify,#btnDelete,#btnCancle").attr("disabled", true);
		$("#btnAdd").attr("disabled", false);
		$(fromEleIds).attr("disabled", true);
		clearValues();
	});
	
	//修改
	$("#btnModify").click(function() {
		$("#btnAdd,#btnModify,#btnDelete").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled", false);
		$(fromEleIds).attr("disabled", false);
		isModify = true;
	});
	
	//保存
	$("#btnSave").click(function() {
		saveStockChkSurv();
	});
	
	//删除
	$("#btnDelete").click(function() {
		delStockChkSurv();
	});
}

//清除表单值
function clearValues() {
	$(fromEleIds).each(function() {
		var d_val = $(this).attr("default-value");
		$(this).val(d_val ? d_val : "");
		
	});

	$("tr","#data_list").attr("bgColor","#FFFFFF");
}

function queryList() {
	var pageno=trunPageObj.getCurrentPageNo();//当前第几页
	var pagecount=trunPageObj.getPageCount();//一页多少条
	$.post("m/stock_chk/queryStockChkSurv", {
		pageNo: pageno,
		pageSize : pagecount,
		queryDate : $.trim($("#queryDate").val()),
		stockAccount:$.trim($("#stockAccount").val()),
		stockCode:$.trim($("#stockCode").val()),
		diffType : $$("diffType").value,
		exchangeType : $$("exchangeType").value
	}, function(data) {
		if ($.checkErrorMsg(data) ) {
			var fileList = "";
			if(data.stockChkSurvPage && data.stockChkSurvPage.dataList){
				var dataList = data.stockChkSurvPage.dataList;
				$(dataList).each(function(i) {
					fileList += "<tr class='table_td_font' bgColor='#ffffff' onclick='selectTR(this)' objid='" + dataList[i].id + "'>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap >" + (dataList[i].branchNo ? dataList[i].branchNo:"") + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].exchangeType + "'>" + dataList[i].exchangeType + "</td>";
                    fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap >" + (dataList[i].taCode ? dataList[i].taCode : "") + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].stockAccount + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap >" + dataList[i].stockCode + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap >" + (dataList[i].fundAccount ? dataList[i].fundAccount : "") + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].dzAmount + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].hsAmount + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].diffType + "'>" + dataList[i].diffType + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + (dataList[i].note ? dataList[i].note : "") + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].validDate + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].invalidDate + "</td>";
					fileList += "</tr>";
				});
			}
			$("#data_list").html(fileList);
			trunPageObj.setPageData(data.stockChkSurvPage);
		} 
	}, "json");
}

//选中行
function selectTR(obj) {
	//修改背景色
	$(obj).attr("bgColor","#FAEBD7").siblings().attr("bgColor","#FFFFFF");
	var tds = $(obj).find("td");
	//给控件设值
	$("#hidId").val($(obj).attr("objid"));
	
	$("#branchNoIn,#oldBranchNoIn").val(tds.eq(0).text());
	$("#exchangeTypeIn,#oldExchangeTypeIn").val(tds.eq(1).attr("value"));
    $("#taCodeIn,#oldTaCodeIn").val(tds.eq(2).text());
	$("#stockAccountIn,#oldStockAccountIn").val(tds.eq(3).text());
	$("#stockCodeIn,#oldStockCodeIn").val(tds.eq(4).text());
	$("#fundAccountIn,#oldFundAccountIn").val(tds.eq(5).text());
	$("#dzAmountIn,#oldDzAmountIn").val(tds.eq(6).text());
	$("#hsAmountIn,#oldHsAmountIn").val(tds.eq(7).text());
	$("#diffTypeIn,#oldDiffTypeIn").val(tds.eq(8).attr("value"));
	$("#noteIn,#oldNoteIn").val(tds.eq(9).text());
	
	//修改按钮和控件状态
	$("#btnSave").attr("disabled", true);
	$("#btnAdd,#btnModify,#btnDelete,#btnCancle").attr("disabled", false);
	$(fromEleIds).attr("disabled", true);
}

function changePage(url) {
	if($("#btnSave").attr("disabled") == false) {
		if (!confirm("当前编内容还未保存，是否继续？")) {
			return;
		}
	}
	location.href=BASE_PATH+url;
}

//保存
function saveStockChkSurv() {
	if($.validInput("queryDate", "数据日期", true, "\<\>\'\"", 10)){
		return false;
	}
	if($.validInput("branchNoIn", "营业部", false, "\<\>\'\"", 4)){
		return false;
	}
	if($.validInput("exchangeTypeIn", "交易类别", true)){
		return false;
	}
    if($.validInput("taCodeIn", "TA代码", false, "\<\>\'\"", 4)){
		return false;
	}
	if($.validInput("stockAccountIn", "股东代码", true, "\<\>\'\"", 15)){
		return false;
	}
	if($.validInput("stockCodeIn", "证券代码", true, "\<\>\'\"", 15)){
		return false;
	}
	if($.validInput("fundAccountIn", "资金账号", false, "\<\>\'\"", 10)){
		return false;
	}
    /*
	if($.validInput("clientIdIn", "客户号", false, "\<\>\'\"", 10)){
		return false;
	}
    */
	var dzAmount = $.trim($("#dzAmountIn").val());
	if (dzAmount != ""){
	  if($.validNumber("dzAmountIn", "中登份额", true, 999999999999999)){
      return false;
	  }
	} else {
		dzAmount = 0;
	}
	
	var hsAmount = $.trim($("#hsAmountIn").val());
	if (hsAmount != ""){
	  if($.validNumber("hsAmountIn", "柜台份额", true, 999999999999999)){
		  return false;
	  }
	} else {
		hsAmount = 0;
	}
	
	if($.validInput("diffTypeIn", "差异类型", true)){
		return false;
	}
	if($.validInput("noteIn", "备注", false, "", 3000)){
		return false;
	}
	//alert(dzAmount + ":" + hsAmount);
	//alert($.trim($("#hidId").val()));
	var action = isModify ? "modify" : "add";
	$.post("m/stock_chk/" + action, {
		  id :$.trim($("#hidId").val()),
		  queryDate : $.trim($("#queryDate").val()),
		  branchNoIn :$.trim($("#branchNoIn").val()),
			oldBranchNoIn :$.trim($("#oldBranchNoIn").val()),
			exchangeTypeIn :$.trim($("#exchangeTypeIn").val()),
			oldExchangeTypeIn :$.trim($("#oldExchangeTypeIn").val()),
			stockAccountIn :$.trim($("#stockAccountIn").val()),
			oldStockAccountIn :$.trim($("#oldStockAccountIn").val()),
			stockCodeIn :$.trim($("#stockCodeIn").val()),
			oldStockCodeIn :$.trim($("#oldStockCodeIn").val()),
			fundAccountIn :$.trim($("#fundAccountIn").val()),
			oldFundAccountIn :$.trim($("#oldFundAccountIn").val()),
			taCodeIn :$.trim($("#taCodeIn").val()),
			oldTaCodeIn :$.trim($("#oldTaCodeIn").val()),
			dzAmountIn :dzAmount,
			oldDzAmountIn :$.trim($("#oldDzAmountIn").val()),
			hsAmountIn :hsAmount,
			oldHsAmountIn :$.trim($("#oldHsAmountIn").val()),
			diffTypeIn :$.trim($("#diffTypeIn").val()),
			oldDiffTypeIn :$.trim($("#oldDiffTypeIn").val()),
			noteIn :$.trim($("#noteIn").val()),
			oldNoteIn :$.trim($("#oldNoteIn").val())
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				alert("系统提示：保存成功！");
				$("#btnCancle").click();
				queryList();
			} 
		}, "json");
}

//删除
function delStockChkSurv() {
		if (!confirm("确定要删除记录吗？")) {
		return;
	}
	$.post("m/stock_chk/delete", {
		  id :$.trim($("#hidId").val()),
		  queryDate : $.trim($("#queryDate").val())
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				$("#btnCancle").click();
				queryList();
			} 
		}, "json");
}
function exportList() {
	var obj = window.showModalDialog(BASE_PATH + "page/export_select.jsp","","dialogWidth:200px;dialogHeight:80px;help:no;center:yes;status:no");
	if (obj) {
		//alert(BASE_PATH +"m/stock_chk/exportStockChkSurv?queryDate=" + $.trim($("#queryDate").val()) + "&stockAccount=" + $.trim($("#stockAccount").val()) + "&stockCode=" + $.trim($("#stockCode").val()) + "&diffType=" + $.trim($("#diffType").val()) + "&exchangeType=" + $.trim($("#exchangeType").val()) + "&expType=" + obj);
		window.open(BASE_PATH +"m/stock_chk/exportStockChkSurv?queryDate=" + $.trim($("#queryDate").val()) + "&stockAccount=" + $.trim($("#stockAccount").val()) + "&stockCode=" + $.trim($("#stockCode").val()) + "&diffType=" + $$("diffType").value + "&exchangeType=" + $$("exchangeType").value + "&expType=" + obj);
		//window.open("m/stock_chk/exportStockChkSurv?queryDate=" + $.trim($("#queryDate").val()) + "&expType=" + obj);
	}
}
//下拉多选的onchange函数，此函数名不能改
function onchangeThisValue(thisName){
}
